<template>
  <div id="password" style="margin: 100px 0">
      <div class="doit">
        <p class="title">修改密码</p>
        <div class="one">
          <span>新的密码</span>
          <el-input
            type="password"
            size="mini"
            placeholder="请输入新的密码"
            v-model="newpassword">
          </el-input>
        </div>
        <div class="one">
          <span>确认密码</span>
          <el-input
            type="password"
            size="mini"
            placeholder="请确认密码"
            v-model="truepassword">
          </el-input>
        </div>
        <div class="buttons" @click="chpassword">
          <el-button>确定</el-button>
        </div>
      </div>
  </div>
</template>

<script>
import hash from '@/common/hash.js'
import msgbox from '@/common/msgbox.js'

export default {
  name: 'password',
  data () {
    return {
      newpassword: '',
      truepassword: ''
    }
  },
  methods: {
    chpassword () {
      let pdata = [
        {//  验证password
          val: this.newpassword,
          rules: {//  规则
            required: {
              msg: msgbox.get('Password')
            },
            rangeLength: {
              msg: msgbox.get('PasswordError'),
              args: [6, 15]
            }
          }
        }
      ]
      let ress = this.$g.verify.check(pdata)
      if (ress !== true) {
        this.$message.error(ress)
        return false
      }
      let bdata = [
        {//  验证password
          val: this.truepassword,
          rules: {//  规则
            required: {
              msg: msgbox.get('确认密码不能为空')
            }
          }
        }
      ]
      let resss = this.$g.verify.check(bdata)
      if (resss !== true) {
        this.$message.error(resss)
        return false
      }
      let p = /^[^\u4E00-\u9FA5\uF900-\uFA2D\u0020]{6,15}$/
      if (this.truepassword !== this.newpassword) {
        this.$message.error('密码输入不一致')
      } else if (!p.test(this.truepassword)) {
        this.$message.error('不能输入空格')
      } else {
        let that = this
        this.$g.ajax({
          method: 'post',
          path: 'admins/updatepassword',
          data: {
            password: hash.md5hex(this.truepassword)
          },
          cbSuccess: function (data) {
            that.$message.success('修改成功')
            that.$router.push({
              path: '/'
            })
          },
          cbError: function (data) {
            let msg = msgbox.get(data.msg)
            that.$message.error(msg)
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  text-align: center;
  font-size: 18px;
  font-weight: bold;
}
.doit .one .el-input {
  width: 200px;
  margin-left: 20px;
}
.doit .one {
  margin-top: 20px;
  font-size: 14px;
}
.doit {
  padding: 10px;
  font-size: 16px;
  margin: 0 auto;
  width: 290px;
  p {
    line-height: 30px;
    color: #666;
    span {
      color: #333;
      margin-left: 15px;
    }
  }
}
.el-select  {
  position: relative;
  left: 16px;
  width: 200px;
}
.buttons {
  margin-top: 20px;
  text-align: center;
  button {
    margin: 0 20px;
  }
}
</style>
